consecutive numbers c#

105

consecutive numbers c# -

namespace ConsoleApp7
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter numbers separate by hypen : ");
            var name = Console.ReadLine();
            var numarray = name.Split('-');
            for (var i = 0;i<=numarray.Length-1;i++ )
            {
                if (i>1 && (Convert.ToInt32(numarray[i]) != Convert.ToInt32(numarray[i - 1])+1))
                {
                    Console.WriteLine("Not Consecutive");
                    break;
                }
                if (i == numarray.Length-1)
                {
                    Console.WriteLine("Consecutive");
                }

            }
        }
    }
}

consecutive numbers c# -

using System;

namespace ConsoleApp7
{
    public class Program
    {
        public static void Main(string[] args)
        {
        Console.WriteLine("Enter numbers separate by hypen : ");
        var name = Console.ReadLine();
        var numarray = name.Split('-');
        int firstValue = Convert.ToInt32(numarray[0]);

        bool cons = true;
        for (var i = 0;i<numarray.Length;i++ )
        {
            if (Convert.ToInt32(numarray[i])-i != firstValue)
            {
                cons = false;                   
                break;
            }
        }
        if (cons)
        {
            Console.WriteLine("Consecutive");
        }
        else
        {
            Console.WriteLine("Not Consecutive");
        }
    }
}

Comments

Submit
0 Comments